﻿using System;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Caliburn.Micro;
using Microsoft.Phone.Tasks;
using Wulong.Phone.Qiushi.i18n;
using Wulong.Phone.Qiushi.ViewModels.Messages;

namespace Wulong.Phone.Qiushi.ViewModels
{
    public class MainViewModel : Conductor<IScreen>.Collection.OneActive
    {
        static readonly ILog Log = LogManager.GetLog(typeof(MainViewModel));
        private readonly INavigationService _navigationService;
        private readonly IEventAggregator _events;
        Func<AboutUsViewModel> _aboutUsViewModelFactory;
        Func<SettingsViewModel> _settingsViewModelFactory;
        Func<QiushiSectionsViewModel> _createQiushiSectionViewModel;
        Func<PicstoryListViewModel> _createPicstoryListViewModel;

        public MainViewModel(
                            INavigationService navigationService,
                            IEventAggregator events,
                            Func<AboutUsViewModel> aboutUsViewModelFactory,
                            Func<SettingsViewModel> settingsViewModelFactory,
                            Func<QiushiSectionsViewModel> createQiushiSectionViewModel,
                            Func<PicstoryListViewModel> createPicstoryListViewModel
                            )
        {            
            _navigationService = navigationService;
            _events = events;
            _aboutUsViewModelFactory = aboutUsViewModelFactory;
            _settingsViewModelFactory = settingsViewModelFactory;
            _createQiushiSectionViewModel = createQiushiSectionViewModel;
            _createPicstoryListViewModel = createPicstoryListViewModel;
            _backgroundImage = new ImageBrush();
            _backgroundImage.ImageSource = new BitmapImage(DefaultPlaceholder);
            Log.Info("enter the MainViewModel constructor");
        }

        private static readonly Uri DefaultPlaceholder = new Uri("/images/PanoBackground.jpg", UriKind.Relative);
        private ImageBrush _backgroundImage;
        public ImageBrush BackgroundImage
        {
            get { return _backgroundImage; }
        }

        public QiushiSectionsViewModel QiushiSectionsItem
        {
            get
            {
                var panoSectionItem = _createQiushiSectionViewModel();
                panoSectionItem.DisplayName = AppResources.VM_Main_Category;
                return panoSectionItem;
            }
        }

        public PicstoryListViewModel PicstoryItem
        {
            get
            {
                var panoPicstoryItem = _createPicstoryListViewModel();
                panoPicstoryItem.DisplayName = AppResources.VM_Main_Pcistory;
                return panoPicstoryItem;
            }
        }

        public SettingsViewModel SettingsItem
        {
            get
            {
                var panoSettingsItem = _settingsViewModelFactory();
                panoSettingsItem.DisplayName = AppResources.VM_Main_Settings;
                return panoSettingsItem;
            }
        }

        public AboutUsViewModel AboutItem
        {
            get
            {
                var panoAboutItem = _aboutUsViewModelFactory();
                panoAboutItem.DisplayName = AppResources.VM_Main_About;
                return panoAboutItem;
            }
        }

        protected override void OnInitialize()
        {
            if (Items.Count == 0)
            {
                var panoSectionItem = _createQiushiSectionViewModel();
                panoSectionItem.DisplayName = AppResources.VM_Main_Category;
                Items.Add(panoSectionItem);

                var panoPicstoryItem = _createPicstoryListViewModel();
                panoPicstoryItem.DisplayName = AppResources.VM_Main_Pcistory;
                Items.Add(panoPicstoryItem);

                var panoSettingsItem = _settingsViewModelFactory();
                panoSettingsItem.DisplayName = AppResources.VM_Main_Settings;
                Items.Add(panoSettingsItem);

                var panoAboutItem = _aboutUsViewModelFactory();
                panoAboutItem.DisplayName = AppResources.VM_Main_About;
                Items.Add(panoAboutItem);

                ActivateItem(Items[0]);
            }
            
        }

        public void RefreshCommand()
        {
            _events.Publish(new RefreshListMessage());
        }

        public void QuickShotCommand()
        {
            _events.RequestTask<CameraCaptureTask>();
            _navigationService.UriFor<NewPostViewModel>().Navigate();
        }

        public void NavigateToNewPostView()
        {
            _navigationService.UriFor<NewPostViewModel>()
                .Navigate();
        }

        protected override void OnActivate()
        {
            _events.Subscribe(this);
            base.OnActivate();
        }

        protected override void OnDeactivate(bool close)
        {
            _events.Unsubscribe(this);
            base.OnDeactivate(close);
        }

    }
}